<template>
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    style="min-width: 600px; max-width: 750px"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
      <div class="search-container">
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="设定编号"
              style="width: 310px"
              prop="callSettingCode"
            >
              <el-input
                v-if="!props.isReadonly"
                v-model="formData.callSettingCode"
                placeholder="请输入设定编号"
                clearable
              />
              <span v-else>{{ formData.callSettingCode }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="设定描述"
              style="width: 310px"
              prop="callSettingName"
            >
              <el-input
                v-if="!props.isReadonly"
                v-model="formData.callSettingName"
                placeholder="请输入设定描述"
                clearable
              />
              <span v-else>{{ formData.callSettingName }}</span>
            </el-form-item>
          </el-col>
          <!--          <el-col :span="12">-->
          <!--            <el-form-item-->
          <!--              label="公司"-->
          <!--              style="width: 290px"-->
          <!--              prop="organizationId"-->
          <!--            >-->
          <!--              <SelectOriganization-->
          <!--                v-if="!props.isReadonly"-->
          <!--                :selectData="formData.organizationId"-->
          <!--                :isCheckStrictly="true"-->
          <!--                @change="getSelectIds"-->
          <!--                @change-data="organizationDataChange"-->
          <!--                :isMultiple="false"-->
          <!--              />-->
          <!--              <span v-else>{{ formData.organizationCode }}</span>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
          <el-col :span="12">
            <el-form-item label="工厂" style="width: 310px" prop="plantId">
              <el-select
                v-if="!props.isReadonly"
                @change="plantChange"
                v-model="formData.plantId"
                filterable
                placeholder="请选择"
              >
                <el-option
                  v-for="item in plants"
                  :key="item.id"
                  :label="item.organizationCode"
                  :value="item.id"
                />
              </el-select>
              <span v-else> {{ formData.plantCode }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="呼叫类型"
              style="width: 310px"
              prop="callTypeId"
            >
              <el-select
                v-if="!props.isReadonly"
                v-model="formData.callTypeId"
                @change="callChange"
                filterable
                placeholder="请选择"
              >
                <el-option
                  v-for="item in callTypes"
                  :key="item.id"
                  :label="item.callTypeName"
                  :value="item.id"
                />
              </el-select>
              <span v-else> {{ formData.callTypeName }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="班组" style="width: 310px" prop="crewId">
              <el-select
                v-if="!props.isReadonly"
                v-model="formData.crewId"
                placeholder="请输入班组"
                filterable
                clearable
                @change="changeCrew"
              >
                <el-option
                  v-for="item in crewList"
                  :key="item.id"
                  :label="`${item.crewCode}-${item.crewName}`"
                  :value="item.id"
                />
              </el-select>

              <span v-else>{{ formData.crewCode }}</span>
            </el-form-item>
          </el-col>
          <!--          <el-col :span="12">-->
          <!--            <el-form-item-->
          <!--              label="呼叫休班的人"-->
          <!--              style="width: 310px"-->
          <!--              prop="callOffDuty"-->
          <!--            >-->
          <!--              <el-radio-group-->
          <!--                v-model="formData.callOffDuty"-->
          <!--                v-if="!props.callOffDuty"-->
          <!--              >-->
          <!--                <el-radio :label="1">是</el-radio>-->
          <!--                <el-radio :label="0">否</el-radio>-->
          <!--              </el-radio-group>-->
          <!--              <span v-else>{{ formData.callOffDuty == 1 ? "是" : "否" }}</span>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
          <el-col :span="12">
            <el-form-item
              label="呼叫休班的人"
              style="width: 310px"
              prop="callOffDuty"
            >
              <el-radio-group
                v-model="formData.callOffDuty"
                v-if="!props.callOffDuty"
              >
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
              <span v-else>{{ formData.callOffDuty == 1 ? "是" : "否" }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="是否呼叫"
              style="width: 310px"
              prop="callMethod"
            >
              <el-radio-group
                v-model="formData.callMethod"
                v-if="!props.callMethod"
              >
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
              <span v-else>{{ formData.callMethod == 1 ? "是" : "否" }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="启用状态"
              style="width: 310px"
              prop="callMethod"
            >
              <el-radio-group v-model="formData.enabled" v-if="!props.enabled">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
              <span v-else>{{ formData.callMethod == 1 ? "是" : "否" }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" style="width: 310px" prop="remarks">
              <el-input
                v-if="!props.isReadonly"
                v-model="formData.remarks"
                placeholder="请输入备注"
                clearable
              />
              <span v-else>{{ formData.remarks }}</span>
            </el-form-item>
          </el-col>
        </el-row>
      </div>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "ANDCALLSETTINGADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);
let plants = ref([]);
let callTypes = ref([]);
//班组
const crewList = ref([]);
const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

/** 弹窗显示 **/
const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
  }
);

/** formData定义表单数据 */
const formRef = ref(ElForm);
let formData = reactive({
  id: "",
  callSettingCode: "",
  callSettingName: "",
  organizationId: "",
  organizationCode: "",
  organizationName: "",
  plantId: "",
  plantCode: "",
  callTypeId: "",
  callTypeCode: "",
  callOffDuty: 0,
  callMethod: 0,
  crewId: "",
  crewCode: "",
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  enabled: "",
  deleted: "",
});

/** rules规则 */
const rules = reactive({
  callSettingCode: [
    { required: true, message: "请输入设定编号", trigger: "blur" },
  ],
  callSettingName: [
    { required: true, message: "请输入设定描述", trigger: "blur" },
  ],
  organizationId: [{ required: true, message: "请选择公司", trigger: "blur" }],

  plantId: [{ required: true, message: "请选择工厂", trigger: "blur" }],
  callTypeId: [{ required: true, message: "请输入选择类型", trigger: "blur" }],
  crewId: [{ required: true, message: "请选择班组", trigger: "blur" }],
  enabled: [{ required: true, message: "请选择是否启用", trigger: "blur" }],
});

/** 编辑时赋值 */
watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      Object.assign(formData, props.data);
      if (formData.id) {
        getCallTypes();
        getCrews();
      }
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.callSettingCode = "";
  formData.callSettingName = "";
  formData.organizationId = "";
  formData.organizationCode = "";
  formData.organizationName = "";
  formData.plantId = "";
  formData.plantCode = "";
  formData.callTypeId = "";
  formData.callTypeCode = "";
  formData.crewId = "";
  formData.crewCode = "";
  formData.version = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.enabled = "";
  formData.deleted = "";
}
function getSelectIds(ids) {
  formData.organizationId = ids;
  getPlants();
}
function organizationDataChange(datas: [] | {}) {
  formData.organizationName = datas.organizationName;
  formData.organizationCode = datas.organizationCode;
}
function plantChange() {
  var plant = plants.value.find((item) => {
    return item.id === formData.plantId;
  });
  formData.plantCode = plant.organizationCode;
  getCrews();
  getCallTypes();
}
function callChange() {
  var callType = callTypes.value.find((item) => {
    return item.id === formData.callTypeId;
  });
  console.log("callType", callType);
  formData.callTypeCode = callType.callTypeCode;
}
const loading = ref(false);
onMounted(() => {
  getPlants();
});
const changeCrew = (id: any) => {
  formData.crewId = id;
  crewList.value.forEach((item) => {
    if (item.id === id) {
      formData.crewCode = item.crewCode;
      formData.crewName = item.crewName;
    }
  });
};
async function getCrews() {
  await request({
    url: "mes/mescrew/getCrew",
    method: "post",
    data: {
      plantId: formData.plantId,
      enabled: 1,
    },
  }).then((res) => {
    crewList.value = res.data;
  });
}
async function getPlants() {
  const data = await request({
    url: "/system/sysOrganization/getList",
    method: "post",
    data: { organizationType: 3 },
  });
  plants.value = data.data;
}
async function getCallTypes() {
  const data = await request({
    url: "/andon/andcalltype/querycalltypes",
    method: "post",
    data: { plantId: formData.plantId },
  });
  callTypes.value = data.data;
}

/** 角色保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      console.log("formData", toRaw(formData));
      await request({
        url: "/andon/andcallsetting/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>

<style lang="scss" scoped>
h4 {
  font-weight: 400;
  color: #000;
  padding-left: 10px;
  font-size: 14px;
  line-height: 14px;
}
.formTable {
  :deep(.el-form-item) {
    margin-right: 0 !important;
  }
}
:deep(.el-dialog .el-dialog__body) {
  overflow-y: auto;
  height: 65vh;
}
:deep.el-input.el-input--default.el-input--suffix {
  // 固定宽度
  width: 190px !important;
}
</style>
